import datetime
from stock_a.common.db import stock_info
from stock_a.common.utils import time_util

class StockFeatureSource:
    def __init__(self, code):
        self.day_k_info_list = stock_info.get_all_stock_day_k_info(code)
        self.week_k_info_list = stock_info.get_all_stock_week_k_info(code)
        self.month_k_info_list = stock_info.get_all_stock_month_k_info(code)
        self.quarter_k_info_list = stock_info.get_all_stock_quarter_k_info(code)
        self.year_k_info_list = stock_info.get_all_stock_year_k_info(code)
        self.info_group_idx_list = list()
        self.info_group_debug_list = list()
        day_idx = 0
        week_idx = -1
        month_idx = -1
        quarter_idx = -1
        year_idx = -1
        while day_idx < len(self.day_k_info_list):
            day_k_info = self.day_k_info_list[day_idx]
            day_date = day_k_info[stock_info.DATE_INDEX].date()
            week_date = time_util.nearest_last_monday(day_date)
            month_date = time_util.nearest_last_first_day_of_month(day_date)
            quarter_date = time_util.nearest_last_first_day_of_quarter(day_date)
            year_date = time_util.nearest_last_first_day_of_year(day_date)
            if week_idx < 0:
                # 第一次切换week
                if len(self.week_k_info_list) > 0 and self.week_k_info_list[0][stock_info.DATE_INDEX].date() < week_date:
                    week_idx = 0
            if week_idx >= 0:
                if week_idx+1 < len(self.week_k_info_list) and self.week_k_info_list[week_idx+1][stock_info.DATE_INDEX].date() >= week_date:
                    # 未切换week，本次week idx不用更新
                    pass
                elif week_idx+1 < len(self.week_k_info_list) and self.week_k_info_list[week_idx+1][stock_info.DATE_INDEX].date() < week_date:
                    # 切换week了，week idx需要往后挪
                    while week_idx+1 < len(self.week_k_info_list) and self.week_k_info_list[week_idx+1][stock_info.DATE_INDEX].date() < week_date:
                        week_idx += 1
            if month_idx < 0:
                # 第一次切换month
                if len(self.month_k_info_list) > 0 and self.month_k_info_list[0][stock_info.DATE_INDEX].date() < month_date:
                    month_idx = 0
            if month_idx >= 0:
                if month_idx+1 < len(self.month_k_info_list) and self.month_k_info_list[month_idx+1][stock_info.DATE_INDEX].date() >= month_date:
                    # 未切换month，本次month idx不用更新
                    pass
                elif month_idx+1 < len(self.month_k_info_list) and self.month_k_info_list[month_idx+1][stock_info.DATE_INDEX].date() < month_date:
                    # 切换month了，month idx需要往后挪
                    while month_idx+1 < len(self.month_k_info_list) and self.month_k_info_list[month_idx+1][stock_info.DATE_INDEX].date() < month_date:
                        month_idx += 1
            if quarter_idx < 0:
                # 第一次切换quarter
                if len(self.quarter_k_info_list) > 0 and self.quarter_k_info_list[0][stock_info.DATE_INDEX].date() < quarter_date:
                    quarter_idx = 0
            if quarter_idx >= 0:
                if quarter_idx+1 < len(self.quarter_k_info_list) and self.quarter_k_info_list[quarter_idx+1][stock_info.DATE_INDEX].date() >= quarter_date:
                    # 未切换quarter，本次quarter idx不用更新
                    pass
                elif quarter_idx+1 < len(self.quarter_k_info_list) and self.quarter_k_info_list[quarter_idx+1][stock_info.DATE_INDEX].date() < quarter_date:
                    # 切换quarter了，quarter idx需要往后挪
                    while quarter_idx+1 < len(self.quarter_k_info_list) and self.quarter_k_info_list[quarter_idx+1][stock_info.DATE_INDEX].date() < quarter_date:
                        quarter_idx += 1
            if year_idx < 0:
                # 第一次切换year
                if len(self.year_k_info_list) > 0 and self.year_k_info_list[0][stock_info.DATE_INDEX].date() < year_date:
                    year_idx = 0
            if year_idx >= 0:
                if year_idx + 1 < len(self.year_k_info_list) and self.year_k_info_list[year_idx + 1][
                    stock_info.DATE_INDEX].date() >= year_date:
                    # 未切换year，本次year idx不用更新
                    pass
                elif year_idx + 1 < len(self.year_k_info_list) and self.year_k_info_list[year_idx + 1][
                    stock_info.DATE_INDEX].date() < year_date:
                    # 切换year了，year idx需要往后挪
                    while year_idx + 1 < len(self.year_k_info_list) and self.year_k_info_list[year_idx + 1][
                        stock_info.DATE_INDEX].date() < year_date:
                        year_idx += 1

            self.info_group_idx_list.append((day_date, day_idx, week_idx, month_idx, quarter_idx, year_idx))
            self.info_group_debug_list.append((day_date,
                                               self.day_k_info_list[day_idx][stock_info.DATE_INDEX],
                                               self.week_k_info_list[week_idx][stock_info.DATE_INDEX] if week_idx >= 0 else None,
                                               self.month_k_info_list[month_idx][stock_info.DATE_INDEX] if month_idx >= 0 else None,
                                               self.quarter_k_info_list[quarter_idx][stock_info.DATE_INDEX] if quarter_idx >= 0 else None,
                                               self.year_k_info_list[year_idx][stock_info.DATE_INDEX] if year_idx >= 0 else None))
            day_idx += 1
        # print(self.info_group_debug_list)